import * as types from './types'

export const state = {
  advanceRepayment: {
    repays: [],
    additions: [],
    loading: false,
    conditions: {
      mobile: ''
    },
    pageSize: 10,
    total: 0,
    curPage: 1
  }
}

export const getters = {
  advanceRepayment_repays: state => state.advanceRepayment.repays,
  advanceRepayment_loading: state => state.advanceRepayment.loading,
  advanceRepayment_conditions: state => state.advanceRepayment.conditions,
  advanceRepayment_total: state => state.advanceRepayment.total,
  advanceRepayment_curPage: state => state.advanceRepayment.curPage,
  advanceRepayment_pageSize: state => state.advanceRepayment.pageSize
}

export const actions = {
  getAdvanceRepays({
    commit
  }, payload) {
    commit(types.GET_ADVANCE_REPAYS, payload);
  },
  confirmAdvanceRepay({
    commit
  }, payload) {
    commit(types.CONFIRM_ADVANCE_REPAY, payload);
  }
}

export const mutations = {
  /**
   * 获取可以提前还款的列表
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     page
   *   } 
   */
  [types.GET_ADVANCE_REPAYS](state, {
    vm,
    page
  }) {
    state.advanceRepayment.loading = true;
    const params = {
      limit: state.advanceRepayment.pageSize,
      mobile: state.advanceRepayment.conditions.mobile
    };
    params.start = page ? page : state.advanceRepayment.curPage;

    vm.$ajax.get('/repay/showAdvRepayLoan', {
        params: params
      })
      .then(res => {
        let data = res.data;
        if (data && data.status == vm.$appConfig.success) {
          state.advanceRepayment.repays = data.list;
          state.advanceRepayment.total = data.total;
          state.advanceRepayment.pageSize = data.pageSize;
          state.advanceRepayment.curPage = data.curPage;
          state.advanceRepayment.loading = false;
        } else {
          state.advanceRepayment.repays = [];
          state.advanceRepayment.loading = false;
        }
      })
      .catch(err => {
        state.advanceRepayment.repays = [];
        state.advanceRepayment.loading = false;

        vm.$notify.error({
          title: '失败',
          message: '获取还款列表失败'
        });
        console.error(err);
      });
  },
  /**
   * 确认提前还款
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     row,
   *     cb
   *   } 
   */
  [types.CONFIRM_ADVANCE_REPAY](state, {
    vm,
    row,
    cb
  }) {
    vm.$confirm(`是否确定对【${row.loan_name}】进行提前还款操作？`, '提示', {
        type: 'info'
      })
      .then(() => {
        vm.$ajax.post('/repay/updateAdvRepayForm', vm.$postParams(row))
          .then(res => {
            let data = res.data;
            if (data && data.status == vm.$appConfig.success) {
              vm.$notify({
                title: '提示',
                message: data.msg,
                type: 'success'
              });
              if (cb) {
                cb();
              }
            } else {
              let msg = (data && data.msg) ? data.msg : '发生了一个错误';
              vm.$notify.error({
                title: '错误',
                message: msg
              });
            }
          })
          .catch(err => {
            vm.$notify.error({
              title: '提示',
              message: '发生了一个错误'
            });
            console.error(err);
          });
      })
      .catch(() => {});
  }
}